#ifndef __TDP2_SORT_HPP__
#define __TDP2_SORT_HPP__

/*-
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: src/sys/libkern/qsort.c,v 1.10 1999/08/28 00:46:35 peter Exp $
 */

namespace TDP
{
	namespace Sorting
	{
		namespace Utilities
		{
			template<class _T>
			inline _T *Med3(_T *a, _T *b, _T *c, int (*cmp)(const _T *, const _T *))
			{
				return cmp(a, b) < 0 ?
					   (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
						  :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
			}

			//inline int terMin(int a, int b)
			//{
			//	return (a) < (b) ? a : b;
			//}

			inline size_t Min(size_t a, size_t b)
			{
				return (a) < (b) ? a : b;
			}

			template<class _T>
			inline void VecSwap(_T *a, _T *b, size_t n, void (*swap)(_T *a, _T *b))
			{
				while(n)
				{
					swap(a, b);
					a++;
					b++;
					n--;
				}
			}

			template<class _T>
			inline int DefaultCmp(const _T *a, const _T *b)
			{
				return (*a) - (*b);
			}

			template<class _T>
			inline void DefaultSwap(_T *a, _T *b)
			{
				_T temp;
				temp = *a;
				*a = *b;
				*b = temp;
			}
		}

		/*
		 * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
		 */

		template<class _T>
		inline void Sort(_T *a, size_t n, int (*cmp)(const _T *, const _T *), void (*swap)(_T *, _T *))
		{
			_T *pm, *pl, *pn, *pd, *pc, *pb, *pa;
			size_t d, r;
			int swap_cnt;

			do
			{
				swap_cnt = 0;
				if (n < 7) {
					for (pm = a+1; pm < a + n; pm ++)
						for (pl = pm; pl > a && cmp(pl - 1, pl) > 0;
							 pl --)
							swap(pl, pl - 1);
					return;
				}
				pm = a + (n / 2);
				if (n > 7) {
					pl = a;
					pn = a + (n - 1);
					if (n > 40) {
						d = (n / 8);
						pl = Utilities::Med3(pl, pl + d, pl + 2 * d, cmp);
						pm = Utilities::Med3(pm - d, pm, pm + d, cmp);
						pn = Utilities::Med3(pn - 2 * d, pn - d, pn, cmp);
					}
					pm = Utilities::Med3(pl, pm, pn, cmp);
				}
				swap(a, pm);
				pa = pb = a + 1;

				pc = pd = a + (n - 1);
				for (;;) {
					while (pb <= pc && (r = cmp(pb, a)) <= 0) {
						if (r == 0) {
							swap_cnt = 1;
							swap(pa, pb);
							pa ++;
						}
						pb ++;
					}
					while (pb <= pc && (r = cmp(pc, a)) >= 0) {
						if (r == 0) {
							swap_cnt = 1;
							swap(pc, pd);
							pd --;
						}
						pc --;
					}
					if (pb > pc)
						break;
					swap(pb, pc);
					swap_cnt = 1;
					pb ++;
					pc --;
				}
				if (swap_cnt == 0) {  /* Switch to insertion sort */
					for (pm = a + 1; pm < a + n; pm ++)
						for (pl = pm; pl > a && cmp(pl - 1, pl) > 0;
							 pl --)
							swap(pl, pl - 1);
					return;
				}

				pn = a + n;
				r = Utilities::Min(pa - a, pb - pa);
				Utilities::VecSwap<_T>(a, pb - r, r, swap);
				r = Utilities::Min(pd - pc, pn - pd - 1);
				Utilities::VecSwap<_T>(pb, pn - r, r, swap);
				if ((r = pb - pa) > 1)
					Sort<_T>(a, r, cmp, swap);
				if ((r = pd - pc) > 1) {
					/* Iterate rather than recurse to save stack space */
					a = pn - r;
					n = r;
					continue;
				}
			} while(0);
		/*		qsort(pn - r, r / es, es, cmp);*/
		}

		template<class _T>
		inline void Sort(_T *a, size_t n, void (*swap)(_T *, _T *))
		{
			return Sort<_T>(a, n, Utilities::DefaultCmp, swap);
		}

		template<class _T>
		inline void Sort(_T *a, size_t n, int (*cmp)(const _T *, const _T *))
		{
			return Sort<_T>(a, n, cmp, Utilities::DefaultSwap);
		}

		template<class _T>
		inline void Sort(_T *a, size_t n)
		{
			return Sort<_T>(a, n, Utilities::DefaultCmp, Utilities::DefaultSwap);
		}
	}
};

#endif
